package com.eobard.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Eobard_Thawne
 * @version 1.0
 * @date 2022/7/31 -19:18
 */
//新版MybatisPlus自动填充功能
@Component
public class DateTimeMetaObjectHandler implements MetaObjectHandler {

    //增加时触发
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("createTime")){
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
            this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        }
    }

    //修改时触发
    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")) {
            this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        }
    }
}

